﻿using CC.Extending;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Demo
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {

            string text =HttpGet("http://www.ip138.com", "gbk");
            string ip = parseHtml(text);
            var ok = PingIpOrDomainName("qq.com");
            var conn = IsConnectInternet();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new InternetTest());
            //Application.Run(new SliderGroupDemo());
            Application.Run(new OpenXmlSdkDemo());
            //Application.Run(new RpChartDemo());
        }
        public static string parseHtml(String pageHtml)
        {
            string ip = "";
            Match m = Regex.Match(pageHtml, @"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}");
            if (m.Success)
            {
                MessageBox.Show(m.Value);
            }
            ip = pageHtml;
            return ip;
        }

        public static string HttpGet(string url, string encoding)
        {
            string result = string.Empty;
            try
            {
                Encoding encode = System.Text.Encoding.GetEncoding(encoding); //设置编码
                                                                              //获取HttpWebRequest对象
                HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url);
                wbRequest.Method = "GET";
                //设置用户代理
                wbRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36";
                //获取返回流
                HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse();
                using (Stream responseStream = wbResponse.GetResponseStream())
                {
                    //读取返回流
                    using (StreamReader sReader = new StreamReader(responseStream, encode))
                    {
                        result = sReader.ReadToEnd();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("error");
            }
            return result;
        }
        //static string HttpGet(string url, string encoding)
        //{
        //    Encoding encode = System.Text.Encoding.GetEncoding(encoding);
        //    WebClient MyWebClient = new WebClient();
        //    MyWebClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36");
        //    MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于向Internet资源的请求进行身份验证的网络凭据
        //    Byte[] pageData = MyWebClient.DownloadData("http://www.net.cn/static/customercare/yourip.asp"); //从指定网站下载数据
        //    string pageHtml = encode.GetString(pageData);  //如果获取网站页面采用的是GB2312，则 使用这句
        //    return pageHtml;
        //}


        [DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(int Description, int ReservedValue);

        #region 方法一
        /// <summary>
        /// 用于检查网络是否可以连接互联网,true表示连接成功,false表示连接失败 
        /// </summary>
        /// <returns></returns>
        public static bool IsConnectInternet()
        {
            int Description = 0;
            return InternetGetConnectedState(Description, 0);
        }
        #endregion
        /// <summary>
        /// 用于检查IP地址或域名是否可以使用TCP/IP协议访问(使用Ping命令),true表示Ping成功,false表示Ping失败 
        /// </summary>
        /// <param name="strIpOrDName">输入参数,表示IP地址或域名</param>
        /// <returns></returns>
        public static bool PingIpOrDomainName(string strIpOrDName)
        {
            try
            {
                Ping send = new Ping();
                PingOptions pingOption = new PingOptions();
                pingOption.DontFragment = true;
                string data = "";
                byte[] buffer = Encoding.UTF8.GetBytes(data);
                int intTimeout = 120;
                PingReply reply = send.Send(strIpOrDName, intTimeout, buffer, pingOption);
                return reply.Status == IPStatus.Success;
                
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
